package com.myteam.mystory.json;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import com.myteam.mystory.utils.LogUtils;

public class LoadingData extends AsyncTask<Void, Void, Void>{
	private static final String TAG_STORYID = "storyID";
	private static final String TAG_TITLE = "title";
	private static final String TAG_IMAGE = "image";
	private static final String TAG_DATE = "Date";
	private static final String TAG_DESCRIPTION = "Description";
	private static final String TAG_CATEGORY = "Category";
	private static final String TAG_AUTHOR = "Author";
	private static final String TAG_VIEWCOUNT = "viewCount";
	private static final String TAG_LIKECOUNT = "likeCount";
	private static final String TAG_SYNCDATE = "syncDate";
	private static final String TAG_CHAPTER = "chapter";
	private static final String TAG_TITLE_CHAPTER = "title";
	private static final String TAG_CONTENT = "content";
	
	private final String url = "http://readonline.java-tip.com/getstories.php";
	
	private Context context;
	ProgressDialog dialog;
	JSONObject obj_json;
	String jsonstring = "";
	JSONArray ja = null;
	JSONArray ja1 = null;
	
	Boolean isConnectionExist = false;  //test wifi
	TestWifiConnected cd;
	
	
	public LoadingData(Context context) {
		super();
		this.context = context;
	}

	@Override
	protected void onPreExecute() {
		dialog = new ProgressDialog(context);
		dialog.setTitle("Dialog");
		dialog.setMessage("load data in server...");
		dialog.show();
		super.onPreExecute();
	}
	
	@Override
	protected Void doInBackground(Void... params) {
		cd = new TestWifiConnected(context);
		isConnectionExist = cd.checkMobileInternetConnection();
		if (isConnectionExist) { 											// co internet
			 JSONParser jparser = new JSONParser();
			 jsonstring = jparser.getJSONFromUrl(url);
			 try {
				 HttpClient client = new DefaultHttpClient();
				 HttpUriRequest request = new HttpGet(url);
				 HttpResponse response = client.execute(request);
				 String jsonString = StreamUtils.convertToString(response.getEntity().getContent());
				 JSONObject json = new JSONObject(jsonString);
				 JSONObject json1 = json.getJSONObject("1");
				 
//				 String str_storyID = json1.getString(TAG_STORYID);  			LogUtils.e(str_storyID);
//				 String str_title = json1.getString(TAG_TITLE);   				LogUtils.e(str_title);
//				 String str_image = json1.getString(TAG_IMAGE);   				LogUtils.e(str_image);
//				 String str_date = json1.getString(TAG_DATE);					LogUtils.e(str_date);
//				 String str_description = json1.getString(TAG_DESCRIPTION);		LogUtils.e(str_description);
//				 String str_category = json1.getString(TAG_CATEGORY);			LogUtils.e(str_category);
//				 String str_author = json1.getString(TAG_AUTHOR);				LogUtils.e(str_author);
//				 String str_viewCount = json1.getString(TAG_VIEWCOUNT);			LogUtils.e(str_viewCount);
//				 String str_likecount = json1.getString(TAG_LIKECOUNT);			LogUtils.e(str_likecount);
//				 String str_syncdate = json1.getString(TAG_LIKECOUNT);			LogUtils.e(str_syncdate);
//				 
//				 String str_title_chapter = json1.getJSONObject(TAG_CHAPTER).getJSONObject("1").getString(TAG_TITLE_CHAPTER);  LogUtils.e(str_title_chapter);
				 
				 
			} catch (Exception e) { 
				LogUtils.e("Bug not json");
			}
		} else { 															// mat internet
			LogUtils.e("Bug not connect internet");
		}
		return null;
	}
	
	@Override
	protected void onPostExecute(Void result) {
		dialog.dismiss();
		super.onPostExecute(result);
	}
	
}
